"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
async function chunkedParseJson(jsonString, chunkSize = 10000) {
    if (jsonString.length <= chunkSize) {
        return JSON.parse(jsonString);
    }
    const isArray = jsonString.trim().startsWith('[');
    let result = isArray ? [] : {};
    let position = 1;
    let depth = 0;
    let chunkStart = 1;
    for (let i = 1; i < jsonString.length - 1; i++) {
        const char = jsonString[i];
        if (char === '{' || char === '[')
            depth++;
        if (char === '}' || char === ']')
            depth--;
        if (depth === 0 && (char === ',' || i === jsonString.length - 2)) {
            const chunk = jsonString.slice(chunkStart, char === ',' ? i : i + 1);
            if (chunk.trim()) {
                const parsed = JSON.parse(isArray ? `[${chunk}]` : `{${chunk}}`);
                if (isArray) {
                    result.push(parsed[0]);
                }
                else {
                    Object.assign(result, parsed);
                }
            }
            chunkStart = i + 1;
            if (i % chunkSize === 0) {
                await new Promise(resolve => setImmediate(resolve));
            }
        }
    }
    return result;
}
exports.default = chunkedParseJson;
